package com.snail.mobile.sim.oss.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;

public class FileUtil {
	
	private static final int DEFAULT_BUFFER_SIZE = 1024;
	public static boolean createDir(String destDirName) {
		File dir = new File(destDirName);
		if (dir.exists()) {
			return true;
		}
		if (!destDirName.endsWith(File.separator))
			destDirName = destDirName + File.separator;
	
		if (dir.mkdirs()) {
			return true;
		} else {
			return false;
		}
	}
	
	
	
	public static boolean CreateFile(String destFileName) {
		File file = new File(destFileName);
		if (file.exists()) {
			return false;
		}
		if (destFileName.endsWith(File.separator)) {
			return false;
		}
		if (!file.getParentFile().exists()) {
			if (!file.getParentFile().mkdirs()) {
				return false;
			}
		}
		try {
			if (file.createNewFile()) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static String createTempFile(String prefix, String suffix, String dirName) {
		File tempFile = null;
		try {
			if (dirName == null) {
				tempFile = File.createTempFile(prefix, suffix);
				return tempFile.getCanonicalPath();
			} else {
				File dir = new File(dirName);
				if (!dir.exists()) {
					if (!FileUtil.createDir(dirName)) {
						return null;
					}
				}
				tempFile = File.createTempFile(prefix, suffix, dir);
				return tempFile.getCanonicalPath();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
    public static boolean copyDirectory(String srcDirName, String destDirName,  
            boolean overlay) {  

        File srcDir = new File(srcDirName);  
        if (!srcDir.exists()) {  
            return false;  
        } else if (!srcDir.isDirectory()) {  

            return false;  
        }  

        if (!destDirName.endsWith(File.separator)) {  
            destDirName = destDirName + File.separator;  
        }  
        File destDir = new File(destDirName);  

        if (destDir.exists()) {  

            if (overlay) {  
                new File(destDirName).delete();  
            } else {  

                return false;  
            }  
        } else {  
            if (!destDir.mkdirs()) {  
                return false;  
            }  
        }  
  
        boolean flag = true;  
        File[] files = srcDir.listFiles();  
        for (int i = 0; i < files.length; i++) {  
            if (files[i].isFile()) {  
                flag = FileUtil.copyFile(files[i].getAbsolutePath(),  destDirName + files[i].getName(), overlay);  
                if (!flag)  
                    break;  
            } else if (files[i].isDirectory()) {  
                flag = FileUtil.copyDirectory(files[i].getAbsolutePath(),  destDirName + files[i].getName(), overlay);  
                if (!flag)  
                    break;  
            }  
        }  
        if (!flag) {  
            return false;  
        } else {  
            return true;  
        }  
    }  
    
    
    public static boolean copyFile(String srcFileName, String destFileName,  
            boolean overlay) {  
        File srcFile = new File(srcFileName);  

        if (!srcFile.exists()) {  

            return false;  
        } else if (!srcFile.isFile()) {  

            return false;  
        }  

        File destFile = new File(destFileName);  
        if (destFile.exists()) {  

            if (overlay) {  

                new File(destFileName).delete();  
            }  
        } else {  

            if (!destFile.getParentFile().exists()) {  

                if (!destFile.getParentFile().mkdirs()) {  

                    return false;  
                }  
            }  
        }  

        int byteread = 0; 
        InputStream in = null;  
        OutputStream out = null;  
  
        try {  
            in = new FileInputStream(srcFile);  
            out = new FileOutputStream(destFile);  
            byte[] buffer = new byte[1024];  
  
            while ((byteread = in.read(buffer)) != -1) {  
                out.write(buffer, 0, byteread);  
            }  
            return true;  
        } catch (FileNotFoundException e) {  
            return false;  
        } catch (IOException e) {  
            return false;  
        } finally {  
            try {  
                if (out != null)  
                    out.close();  
                if (in != null)  
                    in.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    
    public static boolean string2File(String res, String filePath) { 
        boolean flag = true; 
        BufferedReader bufferedReader = null; 
        BufferedWriter bufferedWriter = null; 
        try { 
                File distFile = new File(filePath); 
                if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs(); 
                bufferedReader = new BufferedReader(new StringReader(res)); 
                bufferedWriter = new BufferedWriter(new FileWriter(distFile)); 
                char buf[] = new char[1024];         //字符缓冲区 
                int len; 
                while ((len = bufferedReader.read(buf)) != -1) { 
                        bufferedWriter.write(buf, 0, len); 
                } 
                bufferedWriter.flush(); 
                bufferedReader.close(); 
                bufferedWriter.close(); 
        } catch (IOException e) { 
                e.printStackTrace(); 
                flag = false; 
                return flag; 
        } finally { 
                if (bufferedReader != null) { 
                        try { 
                                bufferedReader.close(); 
                        } catch (IOException e) { 
                                e.printStackTrace(); 
                        } 
                } 
        } 
        return flag; 
}
    public static String file2String(File file, String encoding) { 
        InputStreamReader reader = null; 
        StringWriter writer = new StringWriter(); 
        try { 
                if (encoding == null || "".equals(encoding.trim())) { 
                        reader = new InputStreamReader(new FileInputStream(file), encoding); 
                } else { 
                        reader = new InputStreamReader(new FileInputStream(file)); 
                } 
                //将输入流写入输出流 
                char[] buffer = new char[DEFAULT_BUFFER_SIZE]; 
                int n = 0; 
                while (-1 != (n = reader.read(buffer))) { 
                        writer.write(buffer, 0, n); 
                } 
        } catch (Exception e) { 
                e.printStackTrace(); 
                return null; 
        } finally { 
                if (reader != null) 
                        try { 
                                reader.close(); 
                        } catch (IOException e) { 
                                e.printStackTrace(); 
                        } 
        } 
        //返回转换结果 
        if (writer != null) 
                return writer.toString(); 
        else return null; 
}
  
}
